package com.sakhalin.news.ui.news


import androidx.compose.foundation.layout.*
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.sakhalin.news.R
import com.sakhalin.news.models.ItemType
import com.sakhalin.news.models.news.NewsItem
import com.sakhalin.news.ui.theme.BlueDark

@Composable
fun NewsItemView(newsItem: NewsItem, modifier: Modifier = Modifier) {

    val isLightTheme = MaterialTheme.colors.isLight

    Card(elevation = 1.dp) {
        Column(modifier = modifier) {

            if (!newsItem.imageUrl.isNullOrBlank()) {
                AsyncImage(
                    model = ImageRequest.Builder(LocalContext.current)
                        .data(newsItem.imageUrl)
                        .crossfade(true)
                        .build(),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .height(190.dp)
                        .fillMaxWidth(),
                    contentDescription = null
                )
            }

            Column(modifier = Modifier.padding(start = 10.dp, end = 10.dp, top = 10.dp, bottom = 5.dp)) {
                if (newsItem.title != null) {
                    Text(
                        text = newsItem.title,
                        fontSize = 17.sp,
                        fontWeight = if (newsItem.important > 0) FontWeight.Bold else FontWeight.Normal,
                        color = if (newsItem.important > 0) (if (isLightTheme) BlueDark else Color.White) else Color.Unspecified,
                    )
                }

                if (!newsItem.text.isNullOrBlank()) {
                    Text(text = newsItem.text)
                }
            }

            Column(modifier = Modifier.padding(start = 10.dp, end = 10.dp, bottom = 10.dp)) {

                if(newsItem.commentsCount > 0 || newsItem.viewsCount > 0) {
                    Row(
                        horizontalArrangement = Arrangement.End,
                        modifier = Modifier.fillMaxWidth(),
                        verticalAlignment = Alignment.CenterVertically
                    ) {

                        Row(verticalAlignment = Alignment.CenterVertically) {

                            if (newsItem.commentsCount > 0) {
                                IconWithText(
                                    text = " ${newsItem.commentsCount}",
                                    iconId = R.drawable.ic_comments,
                                    modifier = Modifier.padding(end = 5.dp)
                                )
                            }

                            if (newsItem.viewsCount > 0) {
                                IconWithText(
                                    text = " ${newsItem.viewsCount}",
                                    iconId = R.drawable.ic_views_count
                                )
                            }
                        }
                    }
                }

                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 5.dp)
                ) {
                    Text(
                        text = if (newsItem.labelDate.isNotEmpty()) "${newsItem.labelDate} ${newsItem.time}" else newsItem.time,
                        fontSize = 12.sp,
                        color = Color.Gray
                    )
                    Text(text = newsItem.source, fontSize = 12.sp, color = Color.Gray)
                }
            }
        }
    }
}

@Composable
fun IconWithText(text: String, iconId: Int, modifier: Modifier = Modifier) {
    Row(verticalAlignment = Alignment.CenterVertically, modifier = modifier) {
        Icon(
            modifier = Modifier.size(20.dp),
            imageVector = ImageVector.vectorResource(id = iconId),
            tint = Color.LightGray, contentDescription = ""
        )
        Text(text = text, fontSize = 12.sp, color = Color.Gray)
    }
}


@Preview
@Composable
fun NewsItemViewPreview() {
    NewsItemView(
        newsItem = NewsItem(
            id = "1",
            text = "Самое главное, чего постараются достичь в следующем году, — это увеличение поставок социально значимых продуктов питания по прямым договорам от произв...",
            title = "Наценка на продукты в Сахалинской области не должна превысить 32%",
            date = "2022-11-28",
            time = "14:04",
            labelDate = "28 ноября 2022",
            imageUrl = "https://....",
            important = 1,
            commentsCount = 3,
            viewsCount = 13,
            source = "Источник",
            _type = ItemType.Item
        )
    )
}

